module namespace um = 'http://twittercharts.uzh.ch/user-management';

import module namespace http = "http://www.28msec.com/modules/http";
import module namespace xqddf = "http://www.zorba-xquery.com/modules/xqddf";

declare ordered collection um:users as node()*;
declare variable $um:users := xs:QName("um:users");

(:
 : UI for management of the registered Twitter Charts users.
 :)
declare sequential function um:index ()
{
    <html>
        <head>
            <title>Twitter Charts | User Management System</title>
        </head>
        <body>
        <strong>Register a new user:</strong>
        <form method="post" action="register-user">
            <input type="text" name="id" /> &lt; user ID<br />
            <input type="text" name="screen_name" /> &lt; screen name<br />
            <input type="text" name="duration" /> &lt; duration of membership (in days)<br />
            <input type="submit" />
        </form><br />
        <strong>Registered users:</strong>
        {
            for $user in xqddf:collection($um:users)
            return
                <div>
                    user ID: {$user/user_id/text()}<br />
                    screen name: {$user/screen_name/text()}<br />
                    expires: {$user/expires/text()}<br />
                    <a href="delete-user?id={$user/user_id/text()}">[delete]</a><br /><br />
                </div>
        }
        </body>
    </html>
};

(:
 : Registers a new user by creating a new entry in the "users" table or changing
 : the expiration date if the user has already been registered. The user's
 : Twitter ID, Twitter user name and the duration of the membership (in days)
 : have to be provided as POST parameters "id", "screen_name" and "duration". 
 :)
declare sequential function um:register-user ()
{
    declare $user-id := http:get-parameters("id");
    declare $screen-name := http:get-parameters("screen_name");
    declare $duration := <period>P{http:get-parameters("duration")}D</period> cast as xs:duration;
    declare $expires := fn:current-date() + $duration;
    declare $existent := xqddf:collection($um:users)[/user_id/text() eq $user-id];
    
    if ($existent)
    then (
        replace value of node
            $existent/expires
        with
            $expires
    )
    else (
        xqddf:insert-nodes-first(
            $um:users,
            <user>
                <user_id>{$user-id}</user_id>
                <screen_name>{$screen-name}</screen_name>
                <expires>{$expires}</expires>
            </user>
        )
    );
    
    exit returning
        <html>
            Registered successfully!
            <div>
                user ID: {$user-id}<br />
                screen name: {$screen-name}<br />
                expires: {$expires}<br />
            </div>
            <a href="index">[go back]</a>
        </html>;
};

(:
 : Deletes a user from the "users" table, thus removing him from the registered
 : Twitter Charts users.
 :)
declare sequential function um:delete-user ()
{
    declare $user-id := http:get-parameters("id");
    declare $entry := xqddf:collection($um:users)[/user_id/text() eq $user-id];
    
    xqddf:delete-nodes (
        $um:users,
        $entry
    );
    
    exit returning
        <html>
            Deleted successfully!
            <div>
                user ID: {$entry/user_id/text()}<br />
                screen name: {$entry/screen_name/text()}<br />
                expires: {$entry/expires/text()}<br />
            </div>
            <a href="index">[go back]</a>
        </html>;
};
